import { getInfo, getUserinfoId, login } from '@/api/user'
import { resetRouter } from '@/router'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}

  },
  mutations: {
    updataToken(state, token) {
      state.token = token
      setToken(token)
    },
    updataUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    removeuserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async  postToken(context, loginForm) {
      const res = await login(loginForm)
      context.commit('updataToken', res.data)
      return res
    },
    async  postUserInfo(context) {
      const res1 = await getInfo()
      const res2 = await getUserinfoId(res1.data.userId)
      context.commit('updataUserInfo', { ...res1.data, ...res2.data })

      return { ...res1.data, ...res2.data }
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('removeuserInfo')
      resetRouter()
    }

  },
  getters: {

  }
}
